package edu.gatech.stolen.tea;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemDisplayListAdapter extends ArrayAdapter<ToDoItem>{
	
	Context context;
	int layoutResourceId;
	ArrayList<ToDoItem> data;
	ItemCarrier holder;
	
	public ItemDisplayListAdapter (Context context, int layoutResourceId, ArrayList<ToDoItem> data) {
		
		super(context, layoutResourceId, data);
		
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.data = data;
		
	}
	
	public ToDoItem getItem(int position){
		
		return data.get(position);
		
	}
	
	@Override
	public View getView (int position, View view, ViewGroup parent) {
		
		View row = view;
		//ItemHolder holder = null;
		
		if (row == null){
			LayoutInflater inflater = ((Activity)context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);
			
			holder = new ItemCarrier();
			holder.img = (ImageView)row.findViewById(R.id.iItemImageView_itemdisplaylistrow);
			holder.text = (TextView)row.findViewById(R.id.tItemTextView_itemdisplaylistrow);
			holder.check = (CheckBox)row.findViewById(R.id.cbItemCheckBox_itemdisplaylistrow);
		}
		else {
			//holder = (ItemHolder) row.getTag();
		}
		
		final ToDoItem item = data.get(position);
		
		holder.text.setText(item.getName());
		
		if (item.getType().equals(ItemType.PERSONAL)){
			holder.img.setImageResource(R.drawable.personal_item);
		}
		else if (item.getType().equals(ItemType.WORK)){
			holder.img.setImageResource(R.drawable.work_item);
		}
		else if (item.getType().equals(ItemType.SCHOOL)){
			holder.img.setImageResource(R.drawable.school_item);
		}
		
		holder.check.setText("");
		if (item.isDone()) {
			holder.check.setChecked(true);
		}
		else {
			holder.check.setChecked(false);
		}
		
		holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				
				int i = ItemDisplayListActivity.yourItems.indexOf(item);
				if (buttonView.isChecked()){
					ItemManager.editItem(editItemEnum.TYPE, true, item);
				}
				else{
					ItemManager.editItem(editItemEnum.TYPE, false, item);
				}
				
			}
		});
		
		holder.img.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				ItemManager.setCurrentItem(item);
				
			}
			
		});
		
		
		return row;
		
	}
	
	static class ItemCarrier {
		
		ImageView img;
		TextView text;
		CheckBox check;
		
	}

}
